package gui;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * A panel with a text label and a field for data entry data can be obtained as
 * string or as float or as an int
 */
public class ValueEntryPanel extends JPanel {

	/**
	 * The field for the entry of the data value.
	 */
	private JTextField textField;

	/** The label of the panel */
	private JLabel promptLabel;

	/**
	 * The constructor to create the label and text field.
	 * 
	 * @param prompt
	 *            the prompt to appear beside the text field
	 */
	public ValueEntryPanel(String prompt) {
		// fill in the prompt string with space to get an equal size for all
		// prompt/field combos
		while (prompt.length() < 20) {
			prompt += " ";
		}

		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		promptLabel = new JLabel(prompt);
		add(promptLabel);
		add(Box.createHorizontalGlue());

		textField = new JTextField(15);
		add(textField);
		add(Box.createHorizontalGlue());
		textField.setMaximumSize(textField.getPreferredSize());

	}

	/**
	 * Return the data entered as a String.
	 * 
	 * @return the data entered into the text field
	 */
	public String getValueAsString() {
		return textField.getText();
	}

	/**
	 * Return the data entered as an int.
	 * 
	 * @precond an integer value has been entered into the text field
	 * @return the integer entered into the text field
	 * @throws NumberFormatException
	 *             if field does not contain an int
	 */
	public int getValueAsInt() {
		String valueAsString = textField.getText();
		int value = -1;
		if (valueAsString != null && valueAsString.length() > 0) {
			try {
				value = Integer.parseInt(valueAsString);
			} catch (NumberFormatException e) {
				textField.setText("Not an int: " + textField.getText());
				throw e;
			}
		} else {
			textField.setText("Empty field: " + textField.getText());
			throw new NumberFormatException("Didn't enter a value for the int");
		}
		return value;
	}

	/**
	 * Return the data entered as aa float
	 * 
	 * @precond a floating point value has been entered into the text field
	 * @return the float entered into the text field
	 * @throws NumberFormatException
	 *             if contents are not a float
	 */
	public float getValueAsFloat() {
		String valueAsString = textField.getText();
		float value = -1;
		if (valueAsString != null && valueAsString.length() > 0) {
			try {
				value = Float.parseFloat(valueAsString);
			} catch (NumberFormatException e) {
				// textField.setText("Not a float: " + textField.getText());
				NumberFormatException e2 = new NumberFormatException(
						promptLabel.getText() + " is not a number ");
				throw e2;
			}
		} else {
			// textField.setText("Empty field: " + textField.getText());
			throw new NumberFormatException("Didn't enter a value for "
					+ promptLabel.getText());
		}
		return value;
	}

	/**
	 * sets the value that appears in the textbox
	 * 
	 * @param myValue
	 *            String, value to appear in textbox
	 */
	public void setValue(String myValue) {
		textField.setText(myValue);
	}

	public static final long serialVersionUID = 1;
}